package com.bonc.ioc.service.impl;

import com.bonc.ioc.base.page.PageData;
import com.bonc.ioc.base.page.PageResult;
import com.bonc.ioc.base.service.BaseService;
import com.bonc.ioc.common.util.AppReply;
import com.bonc.ioc.dao.HelpMapper;
import com.bonc.ioc.service.HelpService;
import com.bonc.ioc.vo.HelpInfoVo;
import com.bonc.ioc.vo.PageVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindException;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * @author liuyang
 * @creats 2021-04-07-10:50
 */
@Service
public class HelpServiceImpl extends BaseService implements HelpService {
    @Resource
    private HelpMapper helpMapper;

    @Override
    public AppReply save(HelpInfoVo item) {
        if (!StringUtils.isEmpty(item.getId())) {
            if(item.getHelpType() == 1){
                //更新目录
                fillModifyTimeInfo(item);
                return AppReply.success(helpMapper.update(item));
            }else {
                //更新文章
                fillModifyTimeInfo(item);
                return AppReply.success(helpMapper.updateEssay(item));
            }
        } else {
            if(item.getHelpType() == 1){
                //保存目录
                //查询几级目录，查询三级目录0，查询二级返回1或大于一
                Long parentId = item.getParentId();
                if(!StringUtils.isEmpty(parentId)){
                    List<Long> treeIds = helpMapper.selectTreeByParentId(parentId);
                    if(treeIds.get(0) == 0 && treeIds.get(1) != 0){
                        fillBasicTimeInfo(item);
                        fillModifyTimeInfo(item);
                        return AppReply.success(helpMapper.save(item));
                    }else {
                        return AppReply.error("只允许建立三级目录！！！");
                    }
                }else {
                    fillBasicTimeInfo(item);
                    fillModifyTimeInfo(item);
                    return AppReply.success(helpMapper.save(item));
                }
            }else {
                fillBasicTimeInfo(item);
                fillModifyTimeInfo(item);
                return AppReply.success(helpMapper.save(item));
            }

        }
    }

    @Override
    public AppReply deleteByPrimaryKey(String id) {
        Integer childrenNum = helpMapper.selectChildrenById(Long.valueOf(id));
        AppReply state;
        if (childrenNum == 0){
            state  = AppReply.success(helpMapper.delete(id));
        }else {
            state = AppReply.error("该目录存在子目录或帮助文档！！");
        }
        return state;
    }

    @Override
    public HelpInfoVo selectByPrimaryKey(Long id) {
        return helpMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageResult selectHelpInfoByPage(HashMap map) throws BindException {
        HashMap<String, Object> pageParams = this.getPageParams();
        map.putAll(pageParams);
        PageResult pageResult = new PageResult(map, helpMapper.selectByPage(map));
        return pageResult;
    }

    @Override
    public List<HelpInfoVo> getHelp() {
        return helpMapper.getHelp();
    }


    private void fillModifyTimeInfo(HelpInfoVo item) {
        item.setModifyTime(new Date());
    }

    private void fillBasicTimeInfo(HelpInfoVo item) {
        item.setCreateTime(new Date());
    }
}
